﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;  
using System.Text;

public partial class ShowPlayOffs : System.Web.UI.Page
{
    public static PlayoffRounds oQualifyRounds = new PlayoffRounds();
    public static Tournament oTournament;
    public static Boolean IsGenerated = false;
    public static int Pk_Tournament;
    protected void Page_Load(object sender, EventArgs e)
    {
        Pk_Tournament = int.Parse(Request.QueryString["id"]);
        oTournament = new Tournament(Pk_Tournament);
        
        List<Machine> oMachine = Machine.GetMachinesByTournament(Pk_Tournament);
        List<Player> oPlayer = Player.GetPlayersByTournament(Pk_Tournament);
        oQualifyRounds = new PlayoffRounds(Pk_Tournament);
        //oQualifyRounds.Rounds.Sort(new QualifyRoundPlayerComparer(Order.Ascending));
        //oQualifyRounds.Rounds.Sort(new QualifyRoundNumberComparer(Order.Ascending)); 
       // Lotery.MakeQualify(oMachine, oPlayer, oTournament, ref oQualifyRounds);

        oQualifyRounds.Rounds.Sort(new ObjectComparer<PlayoffRound>("PlayerNumber ASC, Round_Number ASC", true));
      
    }
    public string drawTable()
    {
        if(oQualifyRounds.Rounds.Count>0){  
       
            StringBuilder OutPutHTML = new StringBuilder();
            OutPutHTML.Append("<table class=\"Lotery\" style=\"width:780px;text-align:left;border:1px solid black;border-collapse: collapse; padding:5px\">");
            OutPutHTML.Append("<tr style=\"background-color:#ffffff;border:1px solid black; font-weight:bold\">");
            //Header
            OutPutHTML.Append("<td>Player</td>");
            for (int x = 0; x < oTournament.PlayOffsRounds; x++)
            {
                OutPutHTML.AppendFormat("<td>Round {0}</td>", x + 1);
            }
            OutPutHTML.Append("<td style=\"border-left:solid 1px black\">Player</td>");
            for (int x = 0; x < oTournament.PlayOffsRounds; x++)
            {
                OutPutHTML.AppendFormat("<td>Round {0}</td>", x + 1);
            }
            OutPutHTML.Append("</tr>");
            int RowColor = 0;
            int NextPlayer = 0;
            int NextRow = 0;
            //Recorremos todas las rondas
            for (int x = 0; x < oQualifyRounds.Rounds.Count; x++)
            {
                if (NextPlayer == 0 && NextRow == 0)
                {

                    if (RowColor % 2 == 0)
                    {
                        OutPutHTML.Append("<tr style=\"background-color:#c2c2c2\">");
                    }
                    else
                    {
                        OutPutHTML.Append("<tr style=\"background-color:#ffffff\">");
                    }

                    //Pintamos el jugador

                }
                if (NextPlayer == 0)
                {
                    OutPutHTML.AppendFormat("<td style=\"border-left:solid 1px black\">{0}-{1}</td>", oQualifyRounds.Rounds[x].PlayerNumber, oQualifyRounds.Rounds[x].Player_Nick);
                }
                if (oTournament.TournamentType == 0)
                {
                    //siempre se van pintando las máquinas
                    OutPutHTML.AppendFormat("<td>{0}-{1} ({2}º)</td>", oQualifyRounds.Rounds[x].Machine_Number, oQualifyRounds.Rounds[x].Machine_Acron, oQualifyRounds.Rounds[x].PlayInPosition);
                }
                else {
                    OutPutHTML.AppendFormat("<td>{0}-{1}</td>", oQualifyRounds.Rounds[x].Machine_Number, oQualifyRounds.Rounds[x].Machine_Acron);
                }

                NextPlayer++;
                if (NextPlayer >= oTournament.PlayOffsRounds)
                {
                    NextRow++;
                    NextPlayer = 0;
                }

                if (NextRow > 1)
                {
                    NextRow = 0;
                    OutPutHTML.Append("</tr>");
                    RowColor++;
                }



            }



            OutPutHTML.Append("</table>");
            return OutPutHTML.ToString();
        }else{
            return string.Empty; 
        }
        
        
    }
}
